<%@page import="java.text.SimpleDateFormat"%>
<%@page import="examination.InvigilatorBean"%>
<%@page import="examination.ExamBean"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.Date" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="databaseBean" scope="session" class="examination.DatabaseBean"/>
<!DOCTYPE html>
<html>
<%
	String examNo = request.getParameter("examNo");
	if(examNo==null){
		examNo="";//TODO 这里其实是要跳转回上一页的，但是为了方便测试，就不跳转
		//response.sendRedirect(request.getHeader("Referer"));return;
	}
	//查询对应的考试对象
	ExamBean exam = ExamBean.queryByNo(examNo);
	//ExamBean exam = new ExamBean();
	//if(exams!=null&&exams.length!=0){
	//	exam = exams[0];
	//}
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	String date = sdf.format(exam.getDate());
	String applyDate = sdf.format(exam.getApplyDate());
	%>
<head>
	<meta charset="UTF-8">
	<title>考试修改表单</title>
	<link rel="stylesheet" href="layui/css/layui.css">
	<script src="layui/layui.js"></script>
	<script>
		var classNum=0;
		layui.use('form', function(){
		  var form = layui.form;
		  //各种基于事件的操作
		});
		layui.use('laydate',function(){
			var laydate = layui.laydate;
			
			//执行一个laydate实例
			laydate.render({
				elem: '#examDate' //指定元素
				,value:'<%=date%>'
			});
		});
		layui.use('element',function(){
			var element = layui.element;
			element.on('tabDelete(docDemoTabBrief)',function(data){
				classNum--;
			});
		});
	</script>
</head>
<body>
	
	<h2 align=center>考试修改表单</h2>
	<form class="layui-form" action="ExamUpdateServlet" method="post">
		<div class="layui-form-item">
		    <label class="layui-form-label">考试编号</label>
		   	<div class="layui-input-block">
		    	<input type="text" name="examNo" value="<%=exam.getExamNo() %>" required readonly class="layui-input">
		    </div>
	  	</div>
	  	<div class="layui-form-item">
		    <label class="layui-form-label">申请人</label>
		    
		   	<div class="layui-input-block">
		   		<%//查询申请人的姓名
		   		InvigilatorBean appliers = InvigilatorBean.queryByNo(exam.getApplierNo());
		   		//InvigilatorBean applier = new InvigilatorBean();
		   		//if(appliers!=null){
		   		//	applier = appliers[0];
		   		//}
		   		%>
		   		<input type="hidden" name="applierNo" value="<%=appliers.getStaffNo()%>"/>
		    	<input type="text" value="<%=appliers.getStaffNo() +" "+ appliers.getName() %>" required readonly class="layui-input">
		    </div>
	  	</div>
	  	<div class="layui-form-item">
		    <label class="layui-form-label">申请日期</label>
		   	<div class="layui-input-block">
		    	<input type="text" name="applyDate" value="<%=applyDate %>" required readonly  class="layui-input">
		    </div>
	  	</div>
	  	<div class="layui-form-item">
		    <label class="layui-form-label">考试状态</label>
		   	<div class="layui-input-block">
		    	<input type="text" name="status" value="<%=exam.getStatus() %>" required readonly lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
		    </div>
	  	</div>
		<div class="layui-form-item">
		    <label class="layui-form-label">考试日期</label>
		   	<div class="layui-input-block">
		    	<input type="text" name="date" id="examDate" required  lay-verify="required" placeholder="<%=date %>" autocomplete="off" class="layui-input">
		    </div>
	  	</div>
	  	<div class="layui-form-item">
	    <label class="layui-form-label">考试时间</label>
		   	<div class="layui-input-block">
		    	<input type="text" name="time" value="<%=exam.getTime() %>" required  lay-verify="required" placeholder="<%=exam.getTime() %>" autocomplete="off" class="layui-input">
		    </div>
  		</div>
  		<div class="layui-form-item">
		    <label class="layui-form-label">考试课程号</label>
		   	<div class="layui-input-block">
			   	<select name="courseNo" lay-verify="required">
				  	<option value="">请选择考试的课程编号</option>
				  	<%
				  	String sql=String.format("select course_id,course_name from course;");
				  	ResultSet rs=databaseBean.executeQuery(sql);
				  	while(rs.next()){
				  		%>
				  		<option value="<%=rs.getString("course_id") %>" <%=exam.getCourseNo().equals(rs.getString("course_id"))?"selected":"" %>>
				  			<%=rs.getString("course_id")+" "+rs.getString("course_name") %>
				  		</option>
				  		<%
				  	}
				  	rs.close();
				  	%>
			   	</select>
		    </div>
  		</div>
  		<div class="layui-form-item">
	    <label class="layui-form-label">考试地点</label>
		   	<div class="layui-input-block">
		    	<input type="text" name="location" value="<%=exam.getLocation() %>" required  lay-verify="required" placeholder="<%=exam.getLocation() %>" autocomplete="off" class="layui-input">
		    </div>
  		</div>
  		<div class="layui-form-item">
	    <label class="layui-form-label">监考工分</label>
		   	<div class="layui-input-block">
		    	<input type="text" name="workload" value="<%=exam.getWorkload() %>" required  lay-verify="required" placeholder="<%=exam.getWorkload() %>" autocomplete="off" class="layui-input">
		    </div>
  		</div>
  		<div class="layui-form-item">
	    <label class="layui-form-label">副监考人数</label>
		   	<div class="layui-input-block">
		    	<input type="text" name="need" value="<%=exam.getNeed()-1>=0?0:exam.getNeed()-1%>" required  lay-verify="required|number" placeholder="<%=exam.getNeed()-1 %>" autocomplete="off" class="layui-input">
		    </div>
  		</div>
  		<div class="layui-form-item layui-form-text">
	    <label class="layui-form-label">补充说明</label>
	    <div class="layui-input-block">
	    	<textarea name="comment" value="<%=exam.getComment() %>" placeholder="<%=exam.getComment() %>" class="layui-textarea"></textarea>
	    </div>
	    </div>
  		<!-- 班级 -->
  		<div class="layui-form-item">
		    <label class="layui-form-label">修改之前的考试班级</label>
		   	<div class="layui-input-block">
		    	<input type="text" value="<%=String.join(",",exam.getClassNo()) %>" required readonly lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
		    </div>
	  	</div>
  		<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief" lay-allowClose="true">
		  <ul class="layui-tab-title">
		  	<li class="layui-this" id="classLabel0" style="visibility:hidden">货物0</li>
		  </ul>
		  <div class="layui-tab-content">
		  	<!-- 内容模板 -->
		  	<div class="layui-tab-item layui-show" id="class0" style="visibility:hidden">
	    		<!--班级编号  -->
		    	<div class="layui-form-item">
				    <label class="layui-form-label">参考班级</label>
				    <div class="layui-input-block">
				      <select lay-verify="" class="my-classNo"> 
				        <option value="">请选择需要参加考试的班级编号</option>
				        <%
						sql=String.format("SELECT class_id FROM class;");
						rs=databaseBean.executeQuery(sql);
						while(rs.next()){
							%>
							<option value="<%=rs.getString("class_id")%>">
								<%=rs.getString("class_id")%>
							</option>	
							<%
						}
						rs.close();
						%>
				        
				      </select>
				    </div>
  				</div>
		    </div>
		  </div>
		</div> 
  		<!-- 新增班级的动作代码 -->
  		
	    <script>
	   		addClass();
    	
	        function addClass() {
	        	/*复制主体内容*/
	        	classNum++;
	            //获得父节点对象
	            var father = document.getElementById("class0").parentNode;
	            
	            //克隆目标
	            var newClass = document.getElementById("class0").cloneNode(true);

	            //修改ID
	            newClass.id = "class" + classNum;
	            if(classNum!==1){
	            	newClass.classList.remove("layui-show");
	            }
	            
	            newClass.style.visibility="visible";
	          	//为复制元素添加name属性
	            var selectElement = newClass.getElementsByClassName("my-classNo")[0];
	            selectElement.name="classNo";
	            selectElement.setAttribute('lay-verify','required');//设置为必填项
	            
	            //将新元素加入原本的表单中
	            father.appendChild(newClass);
	            
	            /*复制标签内容*/
	            father = document.getElementById("classLabel0").parentNode;
	            var newClass = document.getElementById("classLabel0").cloneNode(true);
	            newClass.id = "classLabel" + classNum;
	            if(classNum!==1){
	            	newClass.classList.remove("layui-this");
	            }
	            
	            
	            newClass.style.visibility="visible";
	            newClass.innerHTML="班级"+ classNum;
	            father.appendChild(newClass);
	            
	            
	          	//更新渲染
	            var element = layui.element;
	            element.render('tab');
	            
	            var form = layui.form;
	            form.render('select');
	            
	        }
	    </script>
	    <div class="layui-form-item" id="submitDiv">
		    <div class="layui-input-block">
		    	<button type="button" class="layui-btn layui-btn-normal" onclick="addClass()">
		    	<i class="layui-icon layui-icon-addition"></i> 
		    	添加新班级
		    	</button>
		      	<button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
		      	<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		    </div>
  		</div>
	</form>
</body>
</html>